<template>
  <a-modal
    :visible="visible"
    title="添加检查记录"
    @cancel="cancel"
    :width="1100"
    @ok="ok"
  >
    <searchData :searchData="searchArr" @search="search" />
    <b-table
      ref="table"
      :columns="columns"
      :request="loadData"
      :selectConfig="{use: true, type: 'checkbox',rowKey: 'id' }"
      @select-click="selectClick"
    />
  </a-modal>
</template>
<script>
import LApi from '@/api/safetySupervision/checkAndRectify/checkRecord'
import moment from 'moment'

export default {
  name: 'addCheckRecord',
  components: {},
  props: {
    visible: false,
    outSelectRows: {
      type: Array,
      default: []
    },
    subcontractorId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      searchArr: [
        {
          name: '检查时间',
          value: 'createTime',
          type: 'dateRange',
          attr: {
            rangeFormat: ['createTimeStart', 'createTimeEnd']
          }
        }
      ],
      columns: [
        { title: '检查区域', dataIndex: 'respAreaStr' },
        { title: '问题等级', dataIndex: 'securityControlEnumStr' },
        { title: '检查时间', dataIndex: 'createTime' }
      ],
      queryParam: {},
      loadData: async p => {
        const q = Object.assign({}, p, { query: this.queryParam })
        q.query.subcontractorId = this.subcontractorId
        if (q.query.createTimeStart) {
          q.query.createTimeStart = q.query.createTimeStart + ' 00:00:00'
          q.query.createTimeEnd = q.query.createTimeEnd + ' 23:59:59'
        }
        let res = await LApi.findListByMainPc({}, q)
        if (res.code === this.SUCCESS_CODE) {
          res.data.map(v => {
            v.disabled = this.outSelectRows.findIndex(w => w.id === v.id) !== -1
          })
          return { code: this.SUCCESS_CODE, data: res.data }
        }
        return { code: this.SUCCESS_CODE, data: [] }
      },
      selectRows: []
    }
  },
  computed: {},
  watch: {
    visible(val) {
      if (val) {
        this.selectRows = []
        this.$refs.table && this.$refs.table.refresh()
      }
    }
  },
  created() {
  },
  methods: {
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh()
    },
    selectClick(e) {
      this.selectRows = e.rows
    },
    ok() {
      if (!this.selectRows.length) {
        return this.$noticeWarning('请选择数据')
      }
      this.$emit('ok', this.selectRows)
      this.cancel()
    },
    cancel() {
      this.$emit('update:visible', false)
    }
  }
}
</script>
